<?php
/**
 * 
 * @author spring
 * @version 2014-6-7 上午8:54:54
 */
class Task_Delivery extends Minion_Task
{
	/**
	 * Generates a help list for all tasks
	 *
	 * @return null
	 */
	
	
	protected $_options = array(
			'start'=>NULL,
			'end'=>NULL,
			'notice'=>NULL,
	);
	//php f:\wamp\www\newerp\index.php --task=delivery --start="2014-03-27 " --end="2014-03-27 23:59:28"
	
	protected function _execute(array $params)
	{
	 set_time_limit(0);
	 ini_set('memory_limit','500M');
	 $start='';
	 $end='';
	if(isset($params['notice'])){
		$start=date('Y-m-d H:i:s',strtotime("-1 days"));
	    $end=date('Y-m-d H:i:s');
       // $start='2014-09-22 15:30:01';
        //$end='2014-09-22 15:59:01';
		}elseif(!isset($params['start'])||!isset($params['end'])){
	 	$start=date('Y-m-d H:i:s',strtotime("-10 minutes"));
	 	$end=date('Y-m-d H:i:s');
	  }else {
	  	$start=$params['start'];
	  	$end=$params['end'];
	  }
	  $i=0;
      $sql_up='';
      $total_rows=0;
       //查找汇率值
	  $defaultrate=DB::select('rate1')
          ->from('rates')
          ->where('name','=','$')
          ->execute('newerp')
          ->get('rate1');
        //查找已发货且不是赛为产品的订单
	  $orders=DB::select('erp_orders_id','currency_type','orders_out_time','sales_account','orders_export_time')
          ->from('erp_orders')
          ->where('orders_out_time','>=',$start)
          ->and_where('orders_out_time','<=',$end)
          ->and_where('orders_status','=',5)
          ->and_where('orders_type','!=',15)
	      ->execute('readonlyerp')
          ->as_array();

	  foreach ($orders as  $order){
          $i++;
          //判断此订单是否已同步过
          $flag=DB::select('order_id')->from('deliveryorders')
              ->where('order_id','=',$order['erp_orders_id'])
              ->execute('newerp')
              ->get('order_id',0);
         if($flag)continue;

	  	if(!$order['currency_type']){
	  		$order['currency_type']=DB::select('currency_type')
                ->from('erp_orders')
                ->where('erp_orders_id','=',$order['erp_orders_id'])
                ->execute('readonlyerp')
                ->get('currency_type',0);
	  	}
	  	$rss=DB::select('orders_products_id','orders_sku','item_price','item_count','orders_item','products_place')
            ->from('erp_orders_products')
            ->where('erp_orders_id','=',$order['erp_orders_id'])
            ->execute('readonlyerp')->as_array();
	  	foreach ($rss as $rs){
            if(trim($rs['orders_sku'])=='')continue;
	  		if(count($rs)){

//                $flag=DB::select('order_id')->from('deliveryorders')->where('orders_products_id','=',$rs['orders_products_id'])->execute('newerp')->get('order_id',0);
//
//                if(!$flag){
                    $single_price=0;
                    //$skuinfo=Basic::load_sku_info($rs['orders_sku']);
                    $rate= DB::select('rate1')->from('rates')->where('name','=',$order['currency_type'])
                        ->and_where('rate_time','<=',$order['orders_out_time'])
                        ->order_by('rate_time','desc' )
                        ->limit(1)
                        ->offset(0)
                        ->execute('newerp')
                        ->get('rate1',$defaultrate);
                    $single_price=$rs['item_price']*$rate;

                    //taj2014-9-24
//	  			$skuinfo=Basic::load_sku_info($rs['orders_sku']);
//	  			$condition['delivery_date']=$order['orders_out_time'];
//	  			$condition['order_id']=$order['erp_orders_id'];
//	  			$condition['orders_products_id']=$rs['orders_products_id'];
//	  			$condition['sku']=trim($rs['orders_sku']);
//	  			$condition['sku_name']=trim($skuinfo['products_name_cn']);
//	  			$condition['sku_standard']=$skuinfo['size'];
//	  			$condition['storage_position']=$skuinfo['products_place'];
//	  			$condition['quantity']=$rs['item_count'];
//	  			$condition['remark']='订单正常销售出库';
//	  			$condition['type']=10;
//
//	  			$rate= DB::select('rate1')->from('rates')->where('name','=',$order['currency_type'])->and_where('rate_time','<=',$order['orders_out_time'])->order_by('rate_time','desc' )->limit(1)->offset(0)->execute('newerp')->get('rate1',$defaultrate);
//	  			$condition['single_price']=$rs['item_price']*$rate;
//
//	  				if(!Delivery::insert_delivery1($condition)){
//	  					echo '订单号为'.$order['erp_orders_id']."入新系统失败[2]\n";
//
//	  				}else{
//							$j++;
//						}
//                    unset($condition);unset($skuinfo);
                    $common = new Service_common();
                    $price = $common->get_sku_price(strtoupper(trim($rs['orders_sku'])));

                    $sql_up .="('".$order['orders_out_time']."','".$order['orders_out_time']."','".$order['erp_orders_id']."','".
                        $rs['orders_products_id']."',1,'".strtoupper(trim($rs['orders_sku']))."','".trim($rs['orders_item']).
                        "','','".$rs['products_place']."','".$rs['item_count']."','".
                        $single_price."',".$rs['item_count']."*".$single_price.",".
                        $price."*".$rs['item_count'].",'订单正常销售出库',10),";

//	  			}else{
//	  				continue;
//	  			}
	  		}else {
	  			echo '订单号为'.$order['erp_orders_id']."没有订单物品[4]\n";
	  		}
	  	}
	  }

        $sql_update="INSERT INTO `deliveryorders`(`delivery_date`, `op_time`, `order_id`, `orders_products_id`,
                        `delivery_man`,`sku`, `sku_name`, `sku_standard`, `storage_position`, `quantity`, `single_price`,
                        `money`, `cost`,  `remark`, `type`) VALUES ";
        if($sql_up!=''){
            $sql_update .= rtrim($sql_up,",");
            list($insert_id, $total_rows) = DB::query(Database::INSERT,$sql_update)->execute('newerp');
        }//echo $sql_up;
        echo '抓的订单发货时间是'.$start.'--'.$end."\n";
        echo '执订单条数是：'.$i."\n";
        echo '此次录入的出库单条数是：'.$total_rows."\n";
        echo '执行结束日期：'.date('Y-m-d H:i:s')."\n";

    }


}
